package com.lancoo.answer.ui.ques;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.lancoo.answer.R;
import com.lancoo.answer.helper.answer.FillAnswerDialog;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.model.eventBean.FillAnswerEventBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;


/**
 * Created by pengmi on 2021/12/15 19:01
 * Description:未对接的空fragment
 */
public class WaitToDockFragment extends Fragment {

    private static final String TAG = "WaitToDockFragment";

    private View inflate;
    private Child child;
    private int typeIndex;
    private int quesIndex;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        inflate = inflater.inflate(R.layout.ev_fragment_not_docking, container, false);
        return inflate;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        List<Type> kindBeanListk = ConstantBean.Companion.getConstantExamBean().getTypeList();
        if(getArguments()==null){
            view.findViewById(R.id.btn_test).setVisibility(View.GONE);
            return;
        }
        typeIndex = getArguments().getInt(AnswerConstant.KEY_TYPEINDEX);
        quesIndex = getArguments().getInt(AnswerConstant.KEY_QUESINDEX);
        Ques ques = kindBeanListk.get(typeIndex).getQuesList().get(quesIndex);
        List<Child> childList = ques.getChildList();
        child = childList.get(0);
        view.findViewById(R.id.btn_test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTest();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    private void showTest(){
        if(child==null){
            return;
        }
        FillAnswerDialog.launchFillAnswerDialog(getParentFragment().getParentFragment(),
                typeIndex,quesIndex,0,0);
    }


    public static Fragment loadFragment(int typeIndex,int queseIndex,int defaultLoadIndex){
        Bundle mBundle=new Bundle();
        mBundle.putInt(AnswerConstant.KEY_TYPEINDEX,typeIndex);
        mBundle.putInt(AnswerConstant.KEY_QUESINDEX,queseIndex);
        mBundle.putInt(AnswerConstant.KEY_LOADCHILDINDEX,defaultLoadIndex);
        WaitToDockFragment waitToDockFragment=new WaitToDockFragment();
        waitToDockFragment.setArguments(mBundle);
        return waitToDockFragment;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEventMainThread(FillAnswerEventBean bean) {
        Log.e(TAG,"typeIndex:"+typeIndex+",quesIndex"+quesIndex);
        Log.e(TAG,"json:"+new Gson().toJson(bean));
        if(typeIndex==bean.getTypeIndex()&&quesIndex==bean.getQuesIndex()){
            Log.e(TAG,"更新样式");
        }
    }
}
